package com.cskaoyan.service.impl;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.keyword.KeywordCreateBO;
import com.cskaoyan.bean.keyword.KeywordListVO;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.po.MarketKeyword;
import com.cskaoyan.bean.po.MarketKeywordExample;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.cskaoyan.service.KeywordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author chenai
 * @description TODO
 * @date 2022-06-07 15:02
 */
@Service
public class KeywordServiceImpl implements KeywordService {
    @Autowired
    MarketKeywordMapper marketKeywordMapper;

    @Override
    public BaseRespVo list(BaseParam baseParam, String keyword, String url) {
        List<MarketKeyword> marketKeywords = new ArrayList<>();
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        MarketKeywordExample example = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(keyword)){
            criteria.andKeywordLike("%"+keyword+"%");
        }
        if (!StringUtils.isEmpty(url)) {
            criteria.andUrlLike("%"+url+"%");
        }
        criteria.andDeletedEqualTo(false);
        example.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        try {
            marketKeywords = marketKeywordMapper.selectByExample(example);
        }catch (Exception e){
            return BaseRespVo.invalidParameter("查询失败");
        }
        PageInfo pageInfo = new PageInfo(marketKeywords);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return BaseRespVo.ok(new KeywordListVO(baseParam.getPage(),baseParam.getLimit(),total,pages,marketKeywords));
    }

    @Override
    public BaseRespVo create(KeywordCreateBO keywordCreateBO) {
        Date date = new Date();
        MarketKeyword marketKeyword = new MarketKeyword();
        marketKeyword.setKeyword(keywordCreateBO.getKeyword());
        marketKeyword.setUrl(keywordCreateBO.getUrl());
        marketKeyword.setIsDefault(keywordCreateBO.getIsDefault());
        marketKeyword.setIsHot(keywordCreateBO.getIsHot());
        marketKeyword.setAddTime(date);
        marketKeyword.setUpdateTime(date);
        marketKeyword.setDeleted(false);
        try {
            marketKeywordMapper.insertSelective(marketKeyword);
        }catch (Exception e){
            return BaseRespVo.invalidParameter("添加失败");
        }
        return BaseRespVo.ok(marketKeyword);
    }

    @Override
    public BaseRespVo update(MarketKeyword marketKeyword) {
        Date date = new Date();
        marketKeyword.setUpdateTime(date);
        try {
            marketKeywordMapper.updateByPrimaryKeySelective(marketKeyword);
        }catch (Exception e){
            return BaseRespVo.invalidParameter("更新失败");
        }
        return BaseRespVo.ok(marketKeyword);
    }

    @Override
    public BaseRespVo delete(MarketKeyword marketKeyword) {
        marketKeyword.setUpdateTime(new Date());
        marketKeyword.setDeleted(true);
        int i = marketKeywordMapper.updateByPrimaryKeySelective(marketKeyword);
        if (i > 0) {
            return BaseRespVo.ok();
        }else {
            return BaseRespVo.invalidParameter("删除失败");
        }
    }

}
